<?php

/*
=====================================================
 Generic Toolbox - by Carl Wells
-----------------------------------------------------
-----------------------------------------------------
 Copyright (c) 2009 Carl Wells
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
=====================================================
 File: system/display/navdisplay.class.php
-----------------------------------------------------
 Purpose: This file builds all visual aspects of the Admin Section.
 All browser output is managed by this file.
=====================================================
*/
class Navdisplay
{

	var $sectorArray;
	
	function Navdisplay(){
		$this->sectorArray = array();
	}
     	// Display the main navigation for the Control Panel
  	// TO BE PULLED FROM A DB LATER A POSSIBILITY
	function display_main_admin_nav(){
	
		$nav = "";
		$nav .="<ul class=\"sexypanels\">\n";
		$nav .="<li><a href=\"index.php?act=sections\">Sections</a></li>\n";
		$nav .="<li><a href=\"index.php?act=promos&amp;sector=1\">Promotions</a></li>\n";
		$nav .="<li><a href=\"index.php?act=pos\">Add POS Item</a></li>\n";
		$nav .="<li><a href=\"index.php?act=users\">Users</a></li>\n";
		$nav .="</ul>\n";
		
		return $nav;
	
	}
	
	function displayPromosNav($page = "index.php?act=promos&amp;"){
        require_once("core/sector.class.php");
		$SECTOR = new Sector();
		$array = $SECTOR->getSectors();
		$html = "\n<div id=\"navcontainer\">\n";
		$html .= "<ul class=\"ui-tabs-nav\">\n";
		
		
		foreach ($array as $value) {
			$html .= "<li ".$this->selectActiveNavLI($value['id'])."><a href=\"".$page."sector=".$value['id']."\" ".$this->selectActiveNavA($value['id'])." ><span>".$value['nameOfSector']."</span></a></li>\n";
			
				}
		$html .= "</ul>\n";
		$html .= "</div>\n";
		echo ($html);
        }
        
        function selectActiveNavA($id){
        	if ($id == $_GET['sector']){
				return "class=\"ui-tabs-selected\"";
			}
        }
        function selectActiveNavLI($id){
        	if ($id == $_GET['sector']){
				return "class=\"ui-tabs-selected\"";
			}
        }
	
	
	function buildNavigation(){
	require_once(SITEBASE."/system/core/sector.class.php");
		require_once(SITEBASE."/system/core/category.class.php");
		require_once(SITEBASE."/system/db/dbPromo.class.php");
		$db = new DbPromo();
     	$CATEGORY = new Category();
   //  	echo ($_SESSION['lob']);
    // breaks up the LOB (if multiple) to show it all
   if(strpos($_SESSION['lob'], ";")){
   
   		$lobs = explode ( ";", $_SESSION['lob'] );
   		$sql = "SELECT `id` FROM `sectors` WHERE `nameOfSector` =";
   	
   	foreach( $lobs as $key => $val){
   		$sql .= "'".$val."' OR `nameOfSector` = ";
   	}
   	
   	// delete the last 5 characters
   	$sql = substr($sql, 0, -20); 
   	
   $sql .= ";";
      
   }else{
     	$sql = "SELECT `id` FROM `sectors` WHERE `nameOfSector` = 'All' OR `nameOfSector` = '".$_SESSION['lob']."' ";
   }
     	
     	$result = $db->selectQuery($sql);
     	while($sectorId = mysql_fetch_assoc($result)){
     		$sector = $sectorId['id'];
     		$sectorArray = array();
			$this->getTheCategories($sector, $CATEGORY, $db);
		}
		
	
			$html .= $this->buildNavFromSectorArray($this->sectorArray, 0);
			
			$html .= "<ul class=\"dropdown dropdown-vertical\" style=\"clear:left;\">\n";
			$html .= "<li class=\"link\"><a href=\"calendar.php\">Calendar</a></li>";
			$html .= "</ul>\n";
		
		return $html;
	}
	
	function buildNavFromSectorArray($sectorArray, $counter) {
	if($counter == 0){
		$html .= "<ul id=\"nav\" class=\"dropdown dropdown-vertical\">\n";
	}else{
		$html .= "\n<ul>\n";
	}
		
  		foreach( $sectorArray as $key => $value ) { 
   
			if ( !array_key_exists('id', $value) ) {
					
					
					if ( $key != "Calendar" ){
						if( !empty($value) && $key != "Calendar"){
							$html .= "<li class=\"dir\">";
						}else{
							$html .= "<li class=\"empty\">";
						}
					
					$html .= "<a href=\"\">$key</a>";
			}else{
					$html .= "<li class=\"link\">";
					$html .= "<a href=\"calendar.php\">$key</a>";
			}
					
					
					if( !empty($value) && $key != "Calendar"){
					$counter++;
					$html .= "";
					$html .= $this->buildNavFromSectorArray( $value, $counter );
					}
					
					$html .= "</li>\n";
			} 
			else{
			//$html .= "<a href=\"#\">".$value['nameOfPromotion']."</a>\n";
			$html .= "<li><a href=\"selectimage.php?sec=ps&amp;category=".$id."&amp;promotion=".$value['id']."\">".$value['nameOfPromotion']."</a></li>\n";

			} 
   
		}
			
		$html .= "</ul>\n";
		
		return $html;
		
	}
	
	function getTheCategories($sector, $CATEGORY, $db){
		$parentResult = $db->selectQuery("SELECT DISTINCT `parent` FROM `categories` WHERE `sectorId` = ".$sector.";");
			// loop through the distinct categories and add to the array
			while($distinctCategories = mysql_fetch_assoc($parentResult)){
				if($distinctCategories['parent'] != NULL){
					$catresult =  $db->selectQuery("SELECT * FROM `categories` WHERE `sectorId` = ".$sector." AND `parent` = '".$distinctCategories['parent']."' ;");
					while($categories = mysql_fetch_assoc($catresult)){
							$this->sectorArray[ $categories['parent'] ][$categories['nameOfCategory']] = $this->getPromotionsForArray($categories['id'], $db);
					}
				}else{
					$catresult =  $db->selectQuery("SELECT * FROM `categories` WHERE `sectorId` = ".$sector." AND `parent` IS NULL ;");
					while($categories = mysql_fetch_assoc($catresult)){
							$this->sectorArray[$categories['nameOfCategory']] = $this->getPromotionsForArray($categories['id'], $db);
					}
				}
			}
	}
	
	function getPromotionsForArray($catId, $db){
		$promoresult =  $db->selectQuery("SELECT * FROM `promotions` WHERE `category` = ".$catId.";");
		$promoArray = array();
		while($promo = mysql_fetch_array($promoresult)){
		
			if($promo['subcategory'] > 0){
				$sql = "SELECT `title` from `subcategories` WHERE `id` = ".$promo['subcategory'].";";
				$sub = $db->selectQuery($sql);
				$subcat = mysql_fetch_array($sub);
				$promoArray[$subcat['title']][] = $promo;
			}else{
				$promoArray[] = $promo;
			}
		}
		
		return $promoArray;
		
	}
	
	function getThesPromosForSector($sector, $CATEGORY, $db, $sectorArray){
		$parentResult = $db->selectQuery("SELECT DISTINCT `parent` FROM `categories` WHERE `sectorId` = ".$sector.";");
		while($distinctCategories = mysql_fetch_assoc($parentResult)){
			$promosArray = array();
			if($distinctCategories['parent'] != NULL && $distinctCategories['parent'] != "" ){
				//print_r($distinctCategories['parent']);
				$this->sectorArray[ $distinctCategories['parent'] ] = array();
				$catresult =  $db->selectQuery("SELECT * FROM `categories` WHERE `sectorId` = ".$sector." AND `parent` = '".$distinctCategories['parent']."' ;");
				$nameOfCat = $distinctCategories['parent'];
			}else{
				$catresult =  $db->selectQuery("SELECT * FROM `categories` WHERE `sectorId` = ".$sector." AND `parent` IS NULL ;");
				$nameOfCat = NULL;
			}
			
			while($val = mysql_fetch_assoc($catresult)){
				$sql = "SELECT * FROM `promotions` WHERE `category` = ".$val['id'].";";
				$res = $db->selectQuery($sql);
				$promotions = mysql_fetch_assoc($res);
				
				while ($promotions = mysql_fetch_assoc($res)){
					if( $val['parent'] == NULL ){
						$promosArray[$val['nameOfCategory']][] = $promotions;
					}
					if( $val['parent'] != NULL ){
					
						// check for subcategory
						echo ("FFFF".$promotions['subcategory']);
						if($promotions['subcategory'] > 0){
						// get which subcategory we need
							$sql = "SELECT `title` from `subcategories` WHERE `id` = ".$promotions['subcategory'].";";
							$sub = $db->selectQuery($sql);
							$subcat = mysql_fetch_array($sub);
							$promosArray[$val['parent']][$subcat['title']] = $promotions;
						}else{
							$promosArray[$val['parent']][] = $promotions;
						}
					}
				}
				
				
			}
			
			$this->sectorArray[] = $promosArray;
		}
		
	}
	
	function getNavLevelOne($sector, $CATEGORY, $db){
	
		$categories = $CATEGORY->getCategories($sector);
		
		$html .= "<ul>\n";
		foreach($categories as $key => $val){
		
		$catsql = "SELECT * FROM `categories_group` WHERE `category_id` = ".$val['id']." AND `sector` = ".$sector."";
		$catRes = $db->selectQuery($catsql);
	//	echo (mysql_num_rows($catRes));
		
		if(mysql_num_rows($catRes) == 0 ){
			$html .= "<li><a href=\"#\">".$val['nameOfCategory']."</a>\n";
			$html .= $this->getNavLevelSub($db, $val['id'], $sector);
			$html .= "</li>\n";
		}else{
			$group = mysql_fetch_array($catRes);
			$groupId = $group['id'];
			$usersql = "SELECT * FROM `users_group` WHERE `groupId` = ".$groupId." AND `userId` = '".$_SESSION['username']."';";
			$userRes = $db->selectQuery($usersql);
			if(mysql_num_rows($userRes) == 1 ){ // user is in the group
				$html .= "<li><a href=\"#\">".$val['nameOfCategory']."</a>\n";
				$html .= $this->getNavLevelSub($db, $val['id'], $sector);
				$html .= "</li>\n";
			}
			
		
		}
			
			
		}
		
		$html .= "</ul><!--END OF UL-->\n";
		
		return $html;
	}
	
	
function getNavLevelSub($db, $id, $sector){
	
	$result = $db->selectPromotions($sector,$id);
	
	if (mysql_num_rows($result) > 0 ) {
	
	$catsArray = $this->getSubcategoryArray($db);
	
	while( $row = mysql_fetch_array($result) ){
	$subCategory = $db->getSubCategoryTitle($row['subcategory']);
		if ($subCategory != ""){
			$catsArray[$subCategory][] = $row;
		}else{
			$catsArray[] = $row;
		}
	}
	
	$html .= $this->buildNav($catsArray, $id);
	
	
	}else{
	$html .= "";
	}
	
	return $html;
	}
	
function buildNav($catsArray,$id){
	
		$html .= "<ul class=\"START OF SECONDARY NAV\">\n";
			foreach ($catsArray as $key => $val){
			if ( is_string($key) ){
					if (!empty($val) ){
					$html .= "<li><a href=\"#\">".$key."</a>\n";
						
						$html .= "<ul>\n";
						$html .= $this->subnav($val, $id);
						$html .= "</ul>\n";
						
					$html .= "</li>\n";
						}
				}else{
					$html .= "<li>";

					$html .= "<a href=\"selectimage.php?sec=ps&amp;category=".$id."&amp;promotion=".$val['id']."\">".$val['nameOfPromotion']."</a>\n";
					$html .= "</li>";
				}
	
			}
			$html .= "</ul><!--END OF SECONDARY NAV-->\n";
	//	print_r($html);
		return $html;
	
	}
	
function subnav($catsArray, $id){
		foreach ($catsArray as $key => $val){
				$html .= "<li>\n";
				$html .= "<a href=\"selectimage.php?sec=ps&category=".$id."&amp;promotion=".$val['id']."\">".$val['nameOfPromotion']."</a>\n";
				$html .= "</li>\n";
				}
			return $html;
	}
	
function getSubcategoryArray($db){
		$sql = "SELECT `title` FROM `subcategories`";
     	$catsArray = array();
     	
     	$catsResult = $db->selectQuery($sql);
     	while ($cats = mysql_fetch_array($catsResult)){
     		$catsArray[$cats['title']] = array();
     	}
     	
     	
     	return $catsArray;
     	}
     	
     	
}

?>